﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PopupControl;
using Utilities;

namespace ManageTicket
{
    public partial class frmMainBody : Form
    {
        #region Contructor
        public frmMainBody()
        {
            InitializeComponent();

            popupToolTip = new Popup(uctrlPopupVoucher = new ManageTicket.Library.Control.PopupVoucher());
            popupToolTip.AutoClose = false;
            popupToolTip.FocusOnOpen = false;
            popupToolTip.ShowingAnimation = popupToolTip.HidingAnimation = PopupAnimations.LeftToRight | PopupAnimations.Slide;
        }
        #endregion

        #region Variable
        Popup popupToolTip;
        Library.Control.PopupVoucher uctrlPopupVoucher;
        clsMethod4All objMethod = new clsMethod4All();
        ManageTicket.Library.Common.CommonFunction clsCommonFunc = new ManageTicket.Library.Common.CommonFunction();

        //report
        //permission
        bool bTonghop = false;
        bool bTaichinh = false;
        bool bCongnoKhachhang = false;
        bool bCongnoNhacungcap = false;
        bool bHoahong = false;
        #endregion

        #region Event
        private void frmMainBody_Load(object sender, EventArgs e)
        {
            GetListVoucher();
            GetListEvent();
            GetListRemind();

            //permission
            clsCommonFunc.GetPermissionReport(ref bTonghop, frmMain.CurrentUser.UserId, "RPT_01");
            clsCommonFunc.GetPermissionReport(ref bTaichinh, frmMain.CurrentUser.UserId, "RPT_02");
            clsCommonFunc.GetPermissionReport(ref bCongnoKhachhang, frmMain.CurrentUser.UserId, "RPT_03");
            clsCommonFunc.GetPermissionReport(ref bCongnoNhacungcap, frmMain.CurrentUser.UserId, "RPT_04");
            clsCommonFunc.GetPermissionReport(ref bHoahong, frmMain.CurrentUser.UserId, "RPT_05");
        }

        private void tabControl1_SelectedTabChanged(object sender, DevComponents.DotNetBar.TabStripTabChangedEventArgs e)
        {
            if (tabVoucher.IsSelected)
            {
                GetListVoucher();
            }
        }

        private void lvwOption_DoubleClick(object sender, EventArgs e)
        {
            if (lvwOption.SelectedItems.Count > 0)
            {
                ListViewItem item = lvwOption.SelectedItems[0];
                if (item.Tag != null)
                {
                    switch (item.Tag.ToString())
                    {
                        //
                        case "bManageTicket":
                            Ticket.frmTicket frmVoucher = new ManageTicket.Ticket.frmTicket();
                            frmVoucher.MdiParent = this.ParentForm;
                            frmVoucher.Show();
                            break;
                        case "bNewTicket":
                            Ticket.frmVoucher frmNewVoucher = new ManageTicket.Ticket.frmVoucher("");
                            //frmNewVoucher.MdiParent = this.ParentForm;
                            frmNewVoucher.ShowDialog();
                            break;
                        case "bInvoice":
                            Ticket.frmInvoice frmInv = new ManageTicket.Ticket.frmInvoice();
                            frmInv.MdiParent = this.ParentForm;
                            frmInv.Show();
                            break;
                        case "bDeposit":
                            Ticket.frmInvoiceDeposit frmInvDe = new ManageTicket.Ticket.frmInvoiceDeposit();
                            frmInvDe.MdiParent = this.ParentForm;
                            frmInvDe.Show();
                            break;


                        case "parCustomer":
                            Partner.frmCustomer frmCus = new ManageTicket.Partner.frmCustomer();
                            frmCus.MdiParent = this.ParentForm;
                            frmCus.Show();
                            break;
                        case "parProvider":
                            Partner.frmProvider frmPro = new ManageTicket.Partner.frmProvider();
                            frmPro.MdiParent = this.ParentForm;
                            frmPro.Show();
                            break;
                        case "parLabourContract":
                            Partner.frmPartnerLabourContract frmLC = new ManageTicket.Partner.frmPartnerLabourContract();
                            frmLC.MdiParent = this.ParentForm;
                            frmLC.Show();
                            break;


                        case "tInvoice":
                            Tools.frmInvoiceOther frmInvOther = new ManageTicket.Tools.frmInvoiceOther();
                            frmInvOther.MdiParent = this.ParentForm;
                            frmInvOther.Show();
                            break;
                        case "tEvent":
                            Tools.frmItem frmEvent = new ManageTicket.Tools.frmItem("EVENT");
                            frmEvent.MdiParent = this.ParentForm;
                            frmEvent.Show();
                            break;
                        case "tRemind":
                            Tools.frmItem frmRemind = new ManageTicket.Tools.frmItem("REMIND");
                            frmRemind.MdiParent = this.ParentForm;
                            frmRemind.Show();
                            break;
                        case "tShare":
                            Tools.frmItem frmShare = new ManageTicket.Tools.frmItem("SHARE");
                            frmShare.MdiParent = this.ParentForm;
                            frmShare.Show();
                            break;


                        case "rptProfit":
                            if (bTonghop)
                            {
                                Report.frmChooseDate rptProfit = new ManageTicket.Report.frmChooseDate("PROFIT");
                                rptProfit.ShowDialog();
                            }
                            else
                                MessageBox.Show("Bạn không có quyền truy xuất báo cáo này.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            break;
                        case "rptFinance":
                            if (bTaichinh)
                            {
                                Report.frmChooseDate rptFinance = new ManageTicket.Report.frmChooseDate("FINANCE");
                                rptFinance.ShowDialog();
                            }
                            else
                                MessageBox.Show("Bạn không có quyền truy xuất báo cáo này.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            break;
                        case "rptCustomer":
                            if (bCongnoKhachhang)
                            {
                                Report.frmDeptReport rptCustomer = new ManageTicket.Report.frmDeptReport("CUSTOMER");
                                rptCustomer.ShowDialog();
                            }
                            else
                                MessageBox.Show("Bạn không có quyền truy xuất báo cáo này.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            break;
                        case "rptProvider":
                            if (bCongnoNhacungcap)
                            {
                                Report.frmDeptReport rptProvider = new ManageTicket.Report.frmDeptReport("PROVIDER");
                                rptProvider.ShowDialog();
                            }
                            else
                                MessageBox.Show("Bạn không có quyền truy xuất báo cáo này.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            break;
                        case "rptCommission":
                            if (bHoahong)
                            {
                                Report.frmDeptReport rptCommission = new ManageTicket.Report.frmDeptReport("COMMISSION");
                                rptCommission.ShowDialog();
                            }
                            else
                                MessageBox.Show("Bạn không có quyền truy xuất báo cáo này.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            break;


                        case "hrDepartment":
                            Human.frmDepartment frmDept = new Human.frmDepartment();
                            frmDept.MdiParent = this.ParentForm;
                            frmDept.Show();
                            break;
                        case "hrPosition":
                            Human.frmPosition frmPos = new Human.frmPosition();
                            frmPos.MdiParent = this.ParentForm;
                            frmPos.Show();
                            break;
                        case "hrEmployee":
                            Human.frmEmployee frmEmp = new Human.frmEmployee();
                            frmEmp.MdiParent = this.ParentForm;
                            frmEmp.Show();
                            break;
                        case "hrGroupUser":
                            Human.frmUsersGroup frmUGroup = new Human.frmUsersGroup();
                            frmUGroup.MdiParent = this.ParentForm;
                            frmUGroup.Show();
                            break;
                        case "hrUser":
                            Human.frmUsers frmUser = new Human.frmUsers();
                            frmUser.MdiParent = this.ParentForm;
                            frmUser.Show();
                            break;
                        case "hrPermission":
                            Human.frmPermission frmPer = new Human.frmPermission();
                            frmPer.MdiParent = this.ParentForm;
                            frmPer.Show();
                            break;


                        case "iCompany":
                            Config.frmCompany frmCom = new ManageTicket.Config.frmCompany();
                            frmCom.MdiParent = this.ParentForm;
                            frmCom.Show();
                            break;
                        case "iItem":
                            Config.DataSetup frmDataSetup = new ManageTicket.Config.DataSetup();
                            frmDataSetup.MdiParent = this.ParentForm;
                            frmDataSetup.Show();
                            break;
                        case "iFunds":
                            Ticket.frmFunds frmFund = new ManageTicket.Ticket.frmFunds();
                            frmFund.MdiParent = this.ParentForm;
                            frmFund.Show();
                            break;
                    }   
                }
                
            }
        }

        private void lvwVoucher_DoubleClick(object sender, EventArgs e)
        {
            if (lvwVoucher.SelectedItems.Count > 0)
            {
                ListViewItem item = lvwVoucher.SelectedItems[0];
                if (item.Tag != null)
                {
                    Business.Voucher boVoucher = new Business.Voucher();
                    DataObjects.Voucher oVou = boVoucher.GetObjectByPK(item.Tag.ToString());
                    if (oVou != null)
                    {
                        clsCommonFunc.AddListVoucher(oVou);
                    }
                    Ticket.frmTicket frmVoucher = new ManageTicket.Ticket.frmTicket();
                    frmVoucher.MdiParent = this.ParentForm;
                    frmVoucher.Show();
                }

            }
        }

        private void lvwVoucher_ItemMouseHover(object sender, ListViewItemMouseHoverEventArgs e)
        {
            if (e.Item.Tag.ToString() != "")
            {
                Business.Voucher boVoucher = new Business.Voucher();
                Business.Customer boCustomer = new Business.Customer();
                DataObjects.Voucher oVou = boVoucher.GetObjectByPK(e.Item.Tag.ToString());
                DataObjects.Customer oCus;
                if (oVou != null)
                {
                    uctrlPopupVoucher.lblVoucherNo.Text = "Số phiếu: " + oVou.VoucherNo;
                    uctrlPopupVoucher.lblDateOfVoucher.Text = "Ngày lập: " + oVou.DateOfVoucher.Value.ToString("dd/MM/yyyy hh:mm tt");
                    uctrlPopupVoucher.lblCustomerId.Text = "Mã khách hàng: " + oVou.CustomerId;
                    oCus = boCustomer.GetObjectByPK(oVou.CustomerId);
                    if (oCus != null)
                        uctrlPopupVoucher.lblCustomerName.Text = "Khách hàng: " + oCus.CustomerName;
                    uctrlPopupVoucher.lblPersonRepresent.Text = "Liên hệ: " + oVou.CustomerPersonRepresent;

                    Rectangle rect = new Rectangle(MousePosition.X, -100, 300, 130);// lvwVoucher.GetCellDisplayRectangle(0, 0, true);
                    popupToolTip.Show(lvwVoucher, rect);
                }
            }
        }
        
        private void lvwVoucher_MouseLeave(object sender, EventArgs e)
        {
            popupToolTip.Close();
        }
        
        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            GetListEvent();
            GetListRemind();
        }

        private void lvwEvent_DoubleClick(object sender, EventArgs e)
        {
            if (lvwEvent.SelectedItems.Count > 0)
            {
                ListViewItem item = lvwEvent.SelectedItems[0];
                if (item.Tag != null)
                {
                    Tools.frmItemDetail frmDetail = new ManageTicket.Tools.frmItemDetail(int.Parse(item.Tag.ToString()));
                    frmDetail.ShowDialog();
                }
            }
        }

        private void lvwRemind_DoubleClick(object sender, EventArgs e)
        {
            if (lvwRemind.SelectedItems.Count > 0)
            {
                ListViewItem item = lvwRemind.SelectedItems[0];
                if (item.Tag != null)
                {
                    Tools.frmItemDetail frmDetail = new ManageTicket.Tools.frmItemDetail(int.Parse(item.Tag.ToString()));
                    frmDetail.ShowDialog();
                }
            }
        }

        #endregion

        #region Method
        private void GetListVoucher()
        {
            lvwVoucher.Items.Clear();
            Business.Voucher boVoucher = new Business.Voucher();
            ArrayList arrResult = boVoucher.GetListByStatus("CURRENT");
            ListViewItem item;
            foreach (DataObjects.Voucher oVou in arrResult)
            {
                item = new ListViewItem();
                item.Tag = oVou.VoucherNo;
                item.Text = oVou.CustomerPersonRepresent + Environment.NewLine + oVou.DateOfVoucher.Value.ToString("dd/MM/yyyy");
                item.ImageIndex = 0;
                lvwVoucher.Items.Add(item);
            }
        }

        private void GetListEvent()
        {
            try
            {
                lvwEvent.Items.Clear();
                Business.Item boItem = new Business.Item();
                int year = monthCalendar1.SelectionStart.Year;
                int month = monthCalendar1.SelectionStart.Month;
                DateTime datefrom = new DateTime(year, month, 1, 0, 0, 0);
                DateTime dateto = new DateTime(year, month, objMethod.GetEndDayOfMonth(month, year), 23, 59, 59);
                ArrayList arr = boItem.GetListWithCondition(datefrom, dateto, "EVENT", true);
                ListViewItem item;
                foreach (DataObjects.Item oItem in arr)
                {
                    item = new ListViewItem();
                    item.ImageIndex = 0;
                    item.Tag = oItem.Id.ToString();
                    item.Text = oItem.DateInput.Value.ToString("dd/MM/yyyy hh:mm tt") + " - " + oItem.Title;
                    lvwEvent.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                //throw ex;
            }
        }

        private void GetListRemind()
        {
            try
            {
                lvwRemind.Items.Clear();
                Business.Item boItem = new Business.Item();
                int year = monthCalendar1.SelectionStart.Year;
                int month = monthCalendar1.SelectionStart.Month;
                DateTime datefrom = new DateTime(year, month, 1, 0, 0, 0);
                DateTime dateto = new DateTime(year, month, objMethod.GetEndDayOfMonth(month, year), 23, 59, 59);
                ArrayList arr = boItem.GetListWithCondition(datefrom, dateto, "REMIND", true);
                ListViewItem item;
                foreach (DataObjects.Item oItem in arr)
                {
                    item = new ListViewItem();
                    item.ImageIndex = 1;
                    item.Tag = oItem.Id.ToString();
                    item.Text = oItem.DateInput.Value.ToString("dd/MM/yyyy hh:mm tt") + " - " + oItem.Title;
                    lvwRemind.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                //throw ex;
            }
        }
        #endregion    
        
    }
}
